<template>
  <div id="app">
    <router-view />
    <!-- 消息通知按钮 -->
    <div class="notification-floating" @click="toggleMessageList">
      <div class="notification-icon" :class="{ 'has-message': hasNewMessage }">
        <i class="el-icon-bell"></i>
      </div>
    </div>

    <!-- 消息列表悬浮窗 -->
    <transition name="fade">
      <div v-if="showMessageList" class="message-list">
        <template v-if="messages.length > 0">
          <div v-for="(message, index) in messages" :key="index" class="message-item" @click="showMessageDetail(message)">
            <div class="message-header">
              <span class="message-title">{{ message.title }}</span>
              <span class="message-time">{{ message.time }}</span>
            </div>
            <div class="message-content">{{ message.context }}</div>
            
          </div>
        </template>
        <div v-else class="no-message">
          暂无消息
        </div>
      </div>
    </transition>

    <!-- 消息详情弹窗 -->
    <el-dialog :title="selectedMessage.title" :visible.sync="showDetail" width="30%">
      <div class="message-detail">
        <div class="message-detail-time">{{ selectedMessage.time }}</div>
        <div class="message-detail-content">{{ selectedMessage.context }}</div>
      </div>
    </el-dialog>
    <theme-picker />
  </div>
</template>

<script>
import ThemePicker from "@/components/ThemePicker";

export default {
  name: "App",
  components: { ThemePicker },
  data() {
    return {
      messages: [],
      showMessageList: false,
      showDetail: false,
      selectedMessage: {},
      hasNewMessage: false
    };
  },
  created() {
    // 从 localStorage 加载消息历史
    const savedMessages = localStorage.getItem('messages');
    if (savedMessages) {
      this.messages = JSON.parse(savedMessages);
      this.hasNewMessage = this.messages.some(msg => !msg.read);
    }

    // 处理待处理的消息
    this.$nextTick(() => {
      if (window.pendingMessages && window.pendingMessages.length > 0) {
        console.log('处理待处理的消息:', window.pendingMessages);
        window.pendingMessages.forEach(msg => {
          this.addMessage(msg.title, msg.context);
        });
        window.pendingMessages = [];
      }
    });
  },
  mounted() {
    // 点击其他区域关闭消息列表
    document.addEventListener('click', this.handleClickOutside);
  },
  beforeDestroy() {
    document.removeEventListener('click', this.handleClickOutside);
  },
  methods: {
    toggleMessageList(event) {
      event.stopPropagation();
      this.showMessageList = !this.showMessageList;
      if (this.showMessageList) {
        // 标记所有消息为已读
        this.messages = this.messages.map(msg => ({ ...msg, read: true }));
        this.hasNewMessage = false;
        // 保存更新后的消息状态
        localStorage.setItem('messages', JSON.stringify(this.messages));
      }
    },
    handleClickOutside(event) {
      const notificationArea = document.querySelector('.notification-floating');
      const messageList = document.querySelector('.message-list');
      if (this.showMessageList && 
          !notificationArea.contains(event.target) && 
          !messageList.contains(event.target)) {
        this.showMessageList = false;
      }
    },
    addMessage(title, context) {
      const message = {
        title,
        context,
        time: new Date().toLocaleString(),
        read: false
      };
      
      // 获取当前存储的消息
      const savedMessages = localStorage.getItem('messages');
      let messages = savedMessages ? JSON.parse(savedMessages) : [];
      
      // 将新消息添加到数组开头
      messages.unshift(message);
      
      // 更新本地存储
      localStorage.setItem('messages', JSON.stringify(messages));
      
      // 更新组件中的消息列表
      this.messages = messages;
      this.hasNewMessage = true;
    },
    showMessageDetail(message) {
      this.selectedMessage = message;
      this.showDetail = true;
    }
  },
  metaInfo() {
    return {
      title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
      titleTemplate: title => {
        return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
      }
    }
  }
};
</script>

<style>
:root {
  /* 主色调 */
  --primary-color: #409EFF;
  --primary-light: #ecf5ff;
  --primary-dark: #337ecc;
  
  /* 辅助色 */
  --success-color: #67C23A;
  --warning-color: #E6A23C;
  --danger-color: #F56C6C;
  --info-color: #909399;
  
  /* 文字颜色 */
  --text-primary: #2c3e50;
  --text-regular: #606266;
  --text-secondary: #909399;
  --text-placeholder: #C0C4CC;
  
  /* 边框颜色 */
  --border-color: #EBEEF5;
  --border-light: #F2F6FC;
  
  /* 背景颜色 */
  --background-color: #f8f9fa;
  --background-light: #ffffff;
  
  /* 阴影 */
  --shadow-light: 0 2px 12px rgba(0, 0, 0, 0.05);
  --shadow-regular: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-dark: 0 6px 16px rgba(0, 0, 0, 0.12);
  
  /* 圆角 */
  --border-radius-small: 4px;
  --border-radius-base: 8px;
  --border-radius-large: 12px;
  
  /* 间距 */
  --spacing-xs: 8px;
  --spacing-sm: 12px;
  --spacing-base: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-regular);
  background-color: var(--background-color);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#app {
  height: 100%;
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: var(--background-light);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}

/* 通用过渡效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 通用卡片样式 */
.common-card {
  background: var(--background-light);
  border-radius: var(--border-radius-base);
  box-shadow: var(--shadow-light);
  transition: all 0.3s ease;
  padding: var(--spacing-base);
}

.common-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-regular);
}

/* 通用按钮样式 */
.common-button {
  padding: 8px 16px;
  border-radius: var(--border-radius-base);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  cursor: pointer;
  border: none;
  outline: none;
}

.common-button:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-light);
}

/* 通用输入框样式 */
.common-input {
  padding: 8px 12px;
  border-radius: var(--border-radius-base);
  border: 1px solid var(--border-color);
  font-size: 14px;
  transition: all 0.3s ease;
  outline: none;
}

.common-input:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px var(--primary-light);
}

/* 通用表格样式 */
.common-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--background-light);
  border-radius: var(--border-radius-base);
  overflow: hidden;
}

.common-table th,
.common-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
  text-align: left;
}

.common-table th {
  background: var(--background-color);
  font-weight: 500;
  color: var(--text-primary);
}

.common-table tr:hover {
  background: var(--primary-light);
}

/* 通用标签样式 */
.common-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--border-radius-small);
  font-size: 12px;
  font-weight: 500;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .common-card {
    padding: var(--spacing-sm);
  }
  
  .common-table th,
  .common-table td {
    padding: 8px;
  }
}

.notification-floating {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #fff;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  z-index: 1000;
}

.notification-icon {
  font-size: 24px;
  color: #909399;
  transition: all 0.3s;
}

.notification-icon.has-message {
  color: #F56C6C;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

.message-list {
  position: fixed;
  bottom: 80px;
  right: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 300px;
  max-height: 400px;
  overflow-y: auto;
  padding: 10px;
  z-index: 999;
}

.message-item {
  padding: 15px;
  border-bottom: 1px solid #EBEEF5;
  background-color: #f5f7fa;
  margin-bottom: 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.message-item:last-child {
  margin-bottom: 0;
}

.message-item:hover {
  background-color: #ecf5ff;
  transform: translateY(-2px);
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.message-title {
  font-weight: bold;
  color: #303133;
  font-size: 14px;
}

.message-time {
  font-size: 12px;
  color: #909399;
}

.message-content {
  color: #606266;
  line-height: 1.5;
  font-size: 13px;
  padding: 10px;
  background-color: white;
  border-radius: 4px;
}

.no-message {
  text-align: center;
  color: #909399;
  padding: 20px 0;
  font-size: 14px;
}

.message-detail {
  padding: 20px;
}

.message-detail-time {
  color: #909399;
  font-size: 12px;
  margin-bottom: 15px;
}

.message-detail-content {
  color: #606266;
  line-height: 1.6;
  font-size: 14px;
  white-space: pre-wrap;
}

/* 自定义消息弹窗样式 */
.message-dialog .el-dialog__body {
  padding: 20px;
}

/* 添加过渡动画 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
  transform: translateY(10px);
}
</style>
